package com.zjsru.oneDay;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 最小时间差
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
 * <p>
 * 输入：timePoints = ["23:59","00:00"]
 * 输出：1
 * <p>
 * 输入：timePoints = ["00:00","23:59","00:00"]
 * 输出：0
 */
public class findMinDifference {
    public int findMinDifference(List<String> timePoints) {
        int n = timePoints.size() * 2;
        int[] nums = new int[n];
        for (int i = 0, idx = 0; i < n / 2; i++, idx += 2) {
            String[] ss = timePoints.get(i).split(":");
            int h = Integer.parseInt(ss[0]), m = Integer.parseInt(ss[1]);
            nums[idx] = h * 60 + m;
            nums[idx + 1] = nums[idx] + 1440;
        }
        Arrays.sort(nums);
        int ans = nums[1] - nums[0];
        for (int i = 0; i < n - 1; i++) {
            ans = Math.min(ans, nums[i + 1] - nums[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        findMinDifference findMinDifference = new findMinDifference();
        List<String> timePoints = new LinkedList<>();
        timePoints.add("23:59");
        timePoints.add("00:00");
        System.out.println(findMinDifference.findMinDifference(timePoints));
    }
}
